<?php


namespace App\Repositories\Policy\Eloquent;


use App\Repositories\Policy\Contracts\QuOrderInterface;
use Curl\CurlService\CurlService;
use Curl\StrService\DateService;
use Curl\StrService\StrService;

class QuOrderRepository implements QuOrderInterface
{
    protected $url = 'https://apiqae.qunar.com/api/router';
    protected $data = [
        'token'=>'7c8df025a26ad2906dda73645ab85b36',
        'key'=>'703ffb6bb0dba6aa9adc3c496d6ade65'
    ];


    public function lists($id='-1',$status = 'TICKET_LOCK'){

        $this->data['type']     = 'incr';
        $this->data['lastId']   = $id;
        $this->data['status']   = $status;

        $res = \Requests::post($this->url,[],$this->data);

        return $this->xmlToArray($res->body);

    }

    /**
     * 按状态获取
     * @param $lastUpdateTime
     * @param $status
     * @return false|mixed
     */
    public function lists2($lastUpdateTime,$status = []){

        $this->data['tag']        = 'flight.national.orderadmin.batch';
        $this->data['createTime'] = DateService::mtime();
        $this->data['params']     = json_encode(['domain'=>'agb.trade.qunar.com','status'=>$status,'lastUpdateTime'=>$lastUpdateTime,'uuid'=>StrService::uuid()]);

        $data = $this->makeSign($this->data);
        $res = CurlService::post($this->url,$data,['content-type:application/x-www-form-urlencoded']);

        if (
            is_array($res)
            && !empty($res['result'])
            && !empty($res['result']['data'])
            && !empty($res['result']['data']['batchResponse'])
        ) {

            return $res['result']['data']['batchResponse'];
        }
        return false;
    }

    /**
     * 生成签名
     * @param $params
     * @return void
     */
    protected function makeSign($params){

        ksort($params);
        $signStr = '';
        foreach ($params as $key=>$value) {

            $signStr .= $key.'='.$value;
        }
        $params['sign'] = md5($signStr);
        return $params;
    }

    /**
     * 获取订单信息
     * @param $orderNo
     * @return mixed
     */
    public function status($orderNo)
    {
        $this->data['tag']        = 'flight.national.orderadmin.accurate';
        $this->data['createTime'] = DateService::mtime();
        $this->data['params']     = json_encode(['domain'=>'agb.trade.qunar.com','orderNo'=>$orderNo,'uuid'=>StrService::uuid()]);

        $data = $this->makeSign($this->data);
        $res = CurlService::post($this->url,$data,['content-type:application/x-www-form-urlencoded']);
        return $res;
    }

    /**
     * 订单详情
     * @param $orderNo
     * @return mixed
     */
    public function orderInfo($orderNo)
    {

        $this->data['tag']        = 'flight.national.orderadmin.accurate';
        $this->data['createTime'] = DateService::mtime();
        $this->data['params']     = json_encode(['domain'=>'agb.trade.qunar.com','orderNo'=>$orderNo,'uuid'=>StrService::uuid()]);

        $data = $this->makeSign($this->data);
        $res = CurlService::post($this->url,$data,['content-type:application/x-www-form-urlencoded']);
        return $res;
    }

    /**
     * xml转数组
     * @param $content
     * @return mixed
     */
    protected function xmlToArray($content){

        $xmlstring = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA);
        return json_decode(json_encode($xmlstring),true);
    }


}